﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

namespace Stopwatch
{
    partial class MainPage
    {
        public MainPage()
        {
            InitializeComponent();
            timer.Interval = new TimeSpan(0, 0, 0, 0, 100);
            timer.Tick += TimerCallback;
        }

        private void TimerCallback(object sender, object e)
        {
            var duration = DateTime.Now - started;
            var s = duration.ToString("h\\:mm\\:ss");
            if (Time.Text !=  s) Time.Text = s;
        }

        DispatcherTimer timer = new DispatcherTimer();
        DateTime started;
        States state = States.Reset;

        private enum States
        {
            Reset = 0,
            Running = 1,
            Stopped = 2
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (state == States.Reset)
            {
                started = DateTime.Now;
                timer.Start();
                Button.Content = "Stop";
                state = States.Running;
                return;
            }
            if (state == States.Running) {
                timer.Stop();
                Button.Content = "Reset";
                state = States.Stopped;
                return;
            }
            if (state == States.Stopped)
            {
                Time.Text = "0:00:00";
                Button.Content = "Start";
                state = States.Reset;
            }
        }

    }
}